package com.fs.ibox_h.web;

import com.alibaba.fastjson.JSON;
import com.fs.ibox_h.pojo.Withdrawa;
import com.fs.ibox_h.servie.impl.withdrawalServiceImpl;
import com.fs.ibox_h.servie.withdrawalService;
import com.fs.ibox_h.util.BeanFactory;
import com.fs.ibox_h.util.DateUtil;
import com.fs.ibox_h.util.ResponseDataUtil;
import com.fs.ibox_h.vo.Condition;
import com.fs.ibox_h.vo.Page;
import com.fs.ibox_h.vo.ResponseData;
import com.fs.ibox_h.vo.ResultEnums;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "withdrawalServlet", value = "/withdrawal/*")
public class withdrawalServlet extends HttpServlet {
    withdrawalService service = BeanFactory.createBean(withdrawalService.class);
    Page<Withdrawa> list ;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        String path = request.getPathInfo();
        String[] paths = path.replaceFirst("/", "").split("/");


        if ("selectByCondition".equalsIgnoreCase(paths[0])) {
            // 根据条件查询
            select(request, response);
        }else if("delete".equalsIgnoreCase(paths[0])){
            delete(request,response);
        }else if("agreeall".equalsIgnoreCase(paths[0])){
            agreeall(request,response);
        }


    }

    //同意所有
    private void agreeall(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Withdrawa> records = list.getRecords();
        PrintWriter writer = response.getWriter();
        ResponseData responseData = null;
        String json = null;
        for (Withdrawa record : records) {
            int i = service.updateById(record.getId());


            if (i>0){
                responseData = ResponseDataUtil.buildOk(i);
            }else {
                responseData = ResponseDataUtil.buildOk(ResultEnums.UPDATE);
            }
            json = JSON.toJSONString(responseData);
            writer.print(json);
            writer.flush();

        }
    }

    //根据条件删除
    private void delete(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);

        int row = service.deleteById(i);

        if (row>0){
            System.out.println("删除成功");
        }
    }

    //带条件查询
    private void select(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int statusInt = 0 ;
        String order = tonull(request.getParameter("order"));
        String user = tonull(request.getParameter("user"));
        String status = request.getParameter("status");
        String date = request.getParameter("date");
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");

        Date date3 = null;


        if (status != null && !status.isEmpty()) {
             statusInt = Integer.parseInt(status);
        }

        if (date != null && !date.isEmpty()) {
            try {
                date3 = DateUtil.toDate(date);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        int currentPageInt = 1;
        int pageSizeInt = 3;

        if (!currentPage.isEmpty() || currentPage!=null){
            currentPageInt = Integer.parseInt(currentPage);
        }
        if (!pageSize.isEmpty() || pageSize!=null){
            pageSizeInt = Integer.parseInt(pageSize);
        }


        Condition condition = new Condition(order, user, statusInt, date3);


        service = BeanFactory.createBean(withdrawalService.class);
        list = service.selectByCondition(currentPageInt, pageSizeInt, condition);

        ResponseData responseData = null;
        if (list != null) {
            responseData = ResponseDataUtil.buildOk(list);

        } else {
            responseData = ResponseDataUtil.buildOk(ResultEnums.LOGIN_FAIL);
        }

        PrintWriter writer = response.getWriter();
        String json = JSON.toJSONString(responseData);
        writer.print(json);
        writer.flush();

    }

    private String tonull(String str) {
        if (str.isEmpty() || str == null) {
            str = null;
        }
        return str;
    }
}
